package mpbe.geometria;

/*
 * Este codigo es software libre y se encuentra licenciado 
 * bajo los términos de la Licencia Pública General de GNU versión 3 
 * según se encuentra publicada por la Free Software Foundation.
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Autor: Martin Pablo Bellanca <martin.bellanca@gmail.com>
 * web: http://mpbe.blogspot.com/
 */

import org.json.simple.JSONObject;

/**
 * @author Martin Pablo Bellanca <martin.bellanca@gmail.com>
 *
 */
public class Vector {
	private double X;
	private double Y;
	
	// Constructores
	public Vector () {X=0; Y=0;}
	public Vector (double X, double Y) {this.X=X; this.Y=Y;}
	public Vector (Punto punto) {this.X = punto.getX(); this.Y = punto.getY();}
	
	
	//*****************************************************
	// geters and setters
	//*****************************************************
	/**
	 * @return the x
	 */
	public double getX() {
		return X;
	}
	/**
	 * @param x the x to set
	 */
	public void setX(double x) {
		X = x;
	}
	/**
	 * @return the y
	 */
	public double getY() {
		return Y;
	}
	public Punto getPunto() {return new Punto(X,Y);}
	/**
	 * @param y the y to set
	 */
	public void setY(double y) {
		Y = y;
	}
	public void setNulo () {X=0; Y=0;}
	public void setUnitario () {X=1; Y=1;}
	public void set(double x, double y) {
		this.X=x;
		this.Y=y;
	}
	public void set(Punto punto){
		this.X=punto.getX();
		this.Y=punto.getY();
	}

	public int getSignoX() {return (int)(X/Math.abs(X));}
	public int getSignoY() {return (int)(Y/Math.abs(Y));}
	
	//*****************************************************
	// delegated
	//*****************************************************
	public void copyInto(Vector vector){
		vector.X = X;
		vector.Y = Y;
	}
	
	
	//*****************************************************
	// Funciones JSON
	//*****************************************************
	public void put_JSON(JSONObject jo){
		X = (Double) jo.get("X");
		Y = (Double) jo.get("Y");
	}
	public JSONObject get_JSONObject() {
		JSONObject jo = new JSONObject();
		jo.put("X", X);
		jo.put("Y", Y);
		return jo;
	}
	public String get_JSONString() {
		String res = "{\"X\":"+X+", ";
		res += "\"Y\":"+Y+"}";
		return res;
	}
	
	
	//*****************************************************
	// funciones sobre el vector
	//*****************************************************
	public double getRadioVector() {return (Math.sqrt(X*X+Y*Y));}
	public double getAngulo() {return Math.atan2(Y, X);}
	public Vector getDirector (double X, double Y) {
		double hip = getRadioVector();
		return new Vector(X/hip, Y/hip);
	}
	public Vector rotar(double anguloRad) {
		double r = getRadioVector();
		double alf = Math.atan2(Y, X)-Math.PI/2;
		return new Vector((Math.sin(alf)*r), (Math.cos(alf)*r));
	}
	public Vector rotar(Punto centro, double anguloRad) {
		Vector p = new Vector(X-centro.getX(), Y-centro.getY());
		p = p.rotar(anguloRad);
		return new Vector(centro.getX()+p.X, centro.getY()+p.Y);
	}
	public Vector perpendicular() {return new Vector (Y, -X);}
	public Vector mover(double x, double y) {return new Vector(this.X+x, this.Y+y);}
	public Vector escalar(double valor) {return new Vector(this.X*valor, this.Y*valor);}
	public Vector escalar(double valorX, double valorY) {return new Vector(this.X*valorX, this.Y*valorY);}
}
